<?php

namespace App\Jobs;

use App\Models\MediaHistory;
use App\Services\Behavior;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Redis;

class LogQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $params;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($params)
    {
        $this->params = $params;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $parms = $this->params;
        $data = $parms['data'];

        $action = $parms['type'];

        $result = $this->$action($data);

        return $result;
    }

    /*
     * 过滤推荐
     * */
    public function filtr_recommend($params)
    {
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        $is_empty_flag = false;
        foreach ($params as $key=>$value){
            if(!in_array($key, ['uid', 'did', 'media_ids']) || (empty(trim($value)) && trim($value) != '0')){
                $is_empty_flag = true;
            }
        }

        if(empty($params) || $is_empty_flag == true)
            return ['status'=>'error', 'message'=>'数据格式有误'];
        if($params['uid'] == '0' && $params['did'] == '0')
            return ['status'=>'error', 'message'=>'无效数据'];
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        if(!empty($params)){
            $put = Behavior::filtr_recommend_write(['uid'=>$params['uid'], 'did'=>$params['did'], 'media_ids'=>$params['media_ids'] ], 'filtr_recommend');
            if($put)
                return ['status'=>'success', 'message'=>'操作成功'];
        }
        return ['status'=>'error', 'message'=>'操作失败'];
    }

    /*
     * 推荐算法
     * */
    public function recommend($params)
    {
        if(!empty($params))
            Behavior::write($params['uid'], $params['media_id'], $params['action'], $params['duration'], $params['mobileMarkId'], $params['province'], $params['city']);
        if(!empty($params['ticket'])){
            $dd = D('media')->where([['id', '=', $params['media_id']]])->increment('pv');
            MediaHistory::addMediaHistory($params['media_id'], $params['ticket']);
        }
        return ['status'=>'success', 'message'=>'操作成功'];
    }
}
